package cn.cleanarch.infra.idp.s3.service.impl;

import cn.cleanarch.infra.idp.commons.utils.Times;
import cn.cleanarch.infra.idp.s3.dao.repository.S3AuditLogRepository;
import cn.cleanarch.infra.idp.s3.domain.entity.S3AuditLog;
import cn.cleanarch.infra.idp.s3.service.S3AuditService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * S3审计服务实现类
 */
@Service
public class S3AuditServiceImpl implements S3AuditService {

    private final S3AuditLogRepository s3AuditLogRepository;

    public S3AuditServiceImpl(S3AuditLogRepository s3AuditLogRepository) {
        this.s3AuditLogRepository = s3AuditLogRepository;
    }

    @Override
    public void logOperation(S3AuditLog auditLog) {
        LocalDateTime now = LocalDateTime.now();
        auditLog.setCreateTime(now);
        auditLog.setUpdateTime(now);
        s3AuditLogRepository.save(auditLog);
    }

    @Override
    public void logUploadOperation(Long userId, String username, String businessType, String fileName, 
                                  Long fileSize, String objectKey, Long s3ConfigId, String ipAddress, 
                                  String userAgent, Long executionTime, String status, String errorMessage) {
        S3AuditLog auditLog = new S3AuditLog();
        auditLog.setUserId(userId);
        auditLog.setUsername(username);
        auditLog.setOperationType("UPLOAD");
        auditLog.setBusinessType(businessType);
        auditLog.setFileName(fileName);
        auditLog.setFileSize(fileSize);
        auditLog.setObjectKey(objectKey);
        auditLog.setS3ConfigId(s3ConfigId);
        auditLog.setIpAddress(ipAddress);
        auditLog.setUserAgent(userAgent);
        auditLog.setExecutionTime(executionTime);
        auditLog.setStatus(status);
        auditLog.setErrorMessage(errorMessage);
        
        logOperation(auditLog);
    }

    @Override
    public void logDeleteOperation(Long userId, String username, String businessType, String objectKey, 
                                  Long s3ConfigId, String ipAddress, String userAgent, 
                                  Long executionTime, String status, String errorMessage) {
        S3AuditLog auditLog = new S3AuditLog();
        auditLog.setUserId(userId);
        auditLog.setUsername(username);
        auditLog.setOperationType("DELETE");
        auditLog.setBusinessType(businessType);
        auditLog.setObjectKey(objectKey);
        auditLog.setS3ConfigId(s3ConfigId);
        auditLog.setIpAddress(ipAddress);
        auditLog.setUserAgent(userAgent);
        auditLog.setExecutionTime(executionTime);
        auditLog.setStatus(status);
        auditLog.setErrorMessage(errorMessage);
        
        logOperation(auditLog);
    }
}